using System;
using System.Collections.Generic;
using System.Text;

namespace SmartMathLibrary
{
    /// <summary>
    /// This class describes a linear factor in form of for example x - 1. 
    /// </summary>
    [Serializable]
    public class LinearFactor : ILinearFactor
    {
        /// <summary>
        /// The summate of the linear factor.
        /// </summary>
        private double summat;

        /// <summary>
        /// The variable name of the linear factor.
        /// </summary>
        private string variableName;

        /// <summary>
        /// Initializes a new instance of the <see cref="LinearFactor"/> class.
        /// </summary>
        /// <param name="summat">The summate of the linear factor.</param>
        public LinearFactor(double summat)
        {
            this.summat = summat;
            this.variableName = "x";
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="LinearFactor"/> class.
        /// </summary>
        /// <param name="factor">The linear factor to clone.</param>
        public LinearFactor(LinearFactor factor)
        {
            if (factor == (LinearFactor) null)
            {
                throw new ArgumentNullException("factor");
            }

            this.summat = factor.Summat;
            this.variableName = factor.VariableName;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="LinearFactor"/> class.
        /// </summary>
        /// <param name="summat">The summate of the linear factor.</param>
        /// <param name="variableName">The variable name of the linear factor.</param>
        public LinearFactor(double summat, string variableName)
        {
            this.summat = summat;
            this.variableName = variableName;
        }

        /// <summary>
        /// Gets or sets the summate of the linear factor.
        /// </summary>
        /// <value>The summate of the linear factor.</value>
        public double Summat
        {
            get { return summat; }
            set { summat = value; }
        }

        /// <summary>
        /// Gets or sets the name of the variable.
        /// </summary>
        /// <value>The name of the variable.</value>
        public string VariableName
        {
            get { return variableName; }
            set { variableName = value; }
        }

        /// <summary>
        /// Converts the linear factor to a polynomial.
        /// </summary>
        /// <returns>The converted linear factor.</returns>
        public SimplePolynomial ToPolynomial()
        {
            return new SimplePolynomial(this.summat, 1);
        }

        /// <summary>
        /// Implements the operator ==.
        /// </summary>
        /// <param name="a">The first linear factor.</param>
        /// <param name="b">The second linear factor.</param>
        /// <returns>The result of the operator.</returns>
        public static bool operator ==(LinearFactor a, LinearFactor b)
        {
            if (System.Object.ReferenceEquals(a, b))
            {
                return true;
            }

            if (((object) a == null) || ((object) b == null))
            {
                return false;
            }

            return ((a.Summat == b.Summat) && (a.VariableName.Equals(b.VariableName)));
        }

        /// <summary>
        /// Implements the operator !=.
        /// </summary>
        /// <param name="a">The first linear factor.</param>
        /// <param name="b">The second linear factor.</param>
        /// <returns>The result of the operator.</returns>
        public static bool operator !=(LinearFactor a, LinearFactor b)
        {
            return !(a == b);
        }

        /// <summary>
        /// Compares the current instance of a linear factor to another.
        /// </summary>
        /// <param name="factor">The factor.</param>
        /// <returns>True if the two factors are even otherwise, false.</returns>
        public bool CompareTo(LinearFactor factor)
        {
            if (factor == (LinearFactor) null)
            {
                throw new ArgumentNullException("factor");
            }

            return this == factor;
        }

        /// <summary>
        /// Determines whether the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <param name="obj">The <see cref="T:System.Object"/> to compare with the current <see cref="T:System.Object"/>.</param>
        /// <returns>
        /// true if the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>; otherwise, false.
        /// </returns>
        /// <exception cref="T:System.NullReferenceException">The <paramref name="obj"/> parameter is null.</exception>
        public override bool Equals(object obj)
        {
            return base.Equals(obj);
        }

        /// <summary>
        /// Serves as a hash function for a particular type.
        /// </summary>
        /// <returns>
        /// A hash code for the current <see cref="T:System.Object"/>.
        /// </returns>
        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        /// <summary>
        /// Returns a <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </returns>
        public override string ToString()
        {
            String result = "(" + variableName;

            if (this.summat < 0)
            {
                result += " - ";
            }
            else
            {
                result += " + ";
            }

            result += ExtendedMath.Fabs(this.summat) + ")";

            return result;
        }
    }
}